package com.eam.eventhandler;

import com.eam.data.EamCheckPlan;
import org.openbravo.base.exception.OBException;
import org.openbravo.base.model.Entity;
import org.openbravo.base.model.ModelProvider;
import org.openbravo.base.model.Property;
import org.openbravo.client.kernel.event.EntityDeleteEvent;
import org.openbravo.client.kernel.event.EntityNewEvent;
import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
import org.openbravo.client.kernel.event.EntityUpdateEvent;
import org.openbravo.dal.core.OBContext;
import org.openbravo.database.ConnectionProvider;
import org.openbravo.erpCommon.utility.Utility;
import org.openbravo.service.db.DalConnectionProvider;
import javax.enterprise.event.Observes;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CheckPlanEventHandler extends EntityPersistenceEventObserver {
	private static Entity[] entities = { ModelProvider.getInstance().getEntity(EamCheckPlan.ENTITY_NAME) };

	@Override
	protected Entity[] getObservedEntities() {
		return entities;
	}
	public void onUpdate(@Observes EntityUpdateEvent event) throws Throwable {
		if (!isValidEvent(event)) {
			return;
		}
		
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//处理时间格式
		
		//获取计划
		EamCheckPlan plan = (EamCheckPlan) event.getTargetInstance();
		// 将要更改的实体类
		final Entity detailsEntity = ModelProvider.getInstance().getEntity(EamCheckPlan.ENTITY_NAME);
		//本次 点检结束时间
		final Property plantype = detailsEntity.getProperty(EamCheckPlan.PROPERTY_NEXTCYCLETIME);
		
		//获取点检周期(数字)
		BigDecimal days = plan.getCycle();
		//获取单位（小时/天）
		String complay = plan.getCompany();
		//获取点检计划当前的状态
		String State = plan.getState();
	//	final Property States = detailsEntity.getProperty(EamCheckPlan.PROPERTY_STATE);
		
				
		//获取点检计划开始时间
		Date planstatetime = plan.getStartTime();
		
		if(State.equals("SPOTCHECK") || State.equals("OK")){
			String language = OBContext.getOBContext().getLanguage().getLanguage();
	        ConnectionProvider conn = new DalConnectionProvider(false);
	        throw new OBException(Utility.messageBD(conn, "该计划不是创建状态，不可更改！", language));
		}else{
			int clycls = days.intValue();
			
			Calendar ca = Calendar.getInstance();
			
			ca.setTime(planstatetime);
			
			if(complay.equals("hour")){
				ca.add(Calendar.HOUR_OF_DAY, clycls);
			}else if(complay.equals("day")){
				ca.add(Calendar.DATE, clycls);// days为增加的天数，可以改变的（点检周期）
			}
			
			planstatetime = ca.getTime();
			 
			Date enddate =  sdf.parse(sdf.format(planstatetime));  //增加了点检周期后的时间

			event.setCurrentState(plantype, enddate);
		}


	}


	public void onSave(@Observes EntityNewEvent event) {
		if (!isValidEvent(event)) {
			return;
		}

		try {


		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//处理时间格式
		//获取计划
		EamCheckPlan plan = (EamCheckPlan) event.getTargetInstance();

		// 将要更改的实体类
		final Entity detailsEntity = ModelProvider.getInstance().getEntity(EamCheckPlan.ENTITY_NAME);
		//本次 点检结束时间
		final Property plantype = detailsEntity.getProperty(EamCheckPlan.PROPERTY_NEXTCYCLETIME);

		//获取点检周期(数字)
		BigDecimal days = plan.getCycle();
		//获取单位（小时/天）
		String complay = plan.getCompany();

		//获取点检计划开始时间
		Date planstatetime = plan.getStartTime();

		int clycls = days.intValue();

		Calendar ca = Calendar.getInstance();

		ca.setTime(planstatetime);

		if(complay.equals("hour")){
			ca.add(Calendar.HOUR_OF_DAY, clycls);
		}else if(complay.equals("day")){
			ca.add(Calendar.DATE, clycls);// days为增加的天数，可以改变的（点检周期）
		}

		planstatetime = ca.getTime();

		Date enddate =  sdf.parse(sdf.format(planstatetime));  //增加了点检周期后的时间
		
		event.setCurrentState(plantype, enddate);
		
		
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	
	public void onDelete(@Observes
	EntityDeleteEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
	}
	
}
